#include "common.h"
#include "global.h"
#include <ctime>

const std::string LogFileName = "log_asgks.log";
const int IntLength = sizeof(int);

const unsigned short TCPListenPort = 3000;
const unsigned short UDPRecvPort = 3001;

const int UDPWait = 500;
const int AgreementWait = 500;
const int NewMemberWait = 500;
const int ExitWait = 1000;
const int MemberListWait = 200;
const int CommonWait = 500;

void notifyWnd(unsigned int type, const std::string &info)
{
	char *cinfo = new char[info.size() + 1];
	memset(cinfo, 0, info.size() + 1);
	memcpy(cinfo, info.c_str(), info.size());
	SendMessageA(gWnd, type, (WPARAM)cinfo, NULL);
}

void notifyWnd(unsigned int type, const std::string &info1, const std::string &info2)
{
	char *cinfo1 = new char[info1.size() + 1];
	char *cinfo2 = new char[info2.size() + 1];
	memset(cinfo1, 0, info1.size() + 1);
	memset(cinfo2, 0, info2.size() + 1);
	memcpy(cinfo1, info1.c_str(), info1.size());
	memcpy(cinfo2, info2.c_str(), info2.size());
	SendMessageA(gWnd, type, (WPARAM)cinfo1, (LPARAM)cinfo2);
}

void newLog(const std::string &log)
{
	if (logstream.good())
	{
		EnterCriticalSection(&filesync);
		time_t currenttime;
		time(&currenttime);
		tm *st = gmtime(&currenttime);
		std::string strtime(ctime(&currenttime));
		logstream << std::endl;
		logstream << "[" << st->tm_hour << ":" << st->tm_min << ":" << st->tm_sec;
		logstream << "  " << (st->tm_year + 1900) << "-" << (st->tm_mon + 1) << ":" << st->tm_mday;
		logstream << "]  " << log << std::endl;
		LeaveCriticalSection(&filesync);
	}
}